using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The UserAddressTableDB class is responsible for interacting with the database to retrieve and store information
/// about UserAddressTable objects.
/// </summary>
public class UserAddressTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of UserAddressTable from the underlying datasource.
/// </summary>
/// <param name="userAddressKey">The unique UserAddressKey of the UserAddressTable in the database.</param>
/// <returns>An UserAddressTable when the UserAddressKey was found in the database, or null otherwise.</returns>
public static UserAddressTable GetItem(int userAddressKey)
{
UserAddressTable myUserAddressTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAddressTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAddressKey", userAddressKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myUserAddressTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myUserAddressTable;
}
}

/// <summary>
/// Returns a list with UserAddressTable objects.
/// </summary>
/// <returns>A generics List with the UserAddressTable objects.</returns>
public static UserAddressTableList GetList()
{
UserAddressTableList tempList = new UserAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAddressTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with UserAddressTable objects.
/// </summary>
/// <returns>A generics List with the UserAddressTable objects.</returns>
public static UserAddressTableList GetListByUserAccountKey(int UserAccountKey)
{
UserAddressTableList tempList = new UserAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAddressTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a UserAddressTable in the database.
/// </summary>
/// <param name="myUserAddressTable">The UserAddressTable instance to save.</param>
/// <returns>The new UserAddressKey if the UserAddressTable is new in the database or the existing UserAddressKey when an item was updated.</returns>
public static int Save(UserAddressTable myUserAddressTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAddressTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myUserAddressTable.UserAddressKey == -1)
{myCommand.Parameters.AddWithValue("@userAddressKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAddressKey", myUserAddressTable.UserAddressKey);
}
if (myUserAddressTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myUserAddressTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myUserAddressTable.UnitNo))
{
myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@unitNo", myUserAddressTable.UnitNo);
}
if (string.IsNullOrEmpty(myUserAddressTable.LevelNo))
{
myCommand.Parameters.AddWithValue("@levelNo", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@levelNo", myUserAddressTable.LevelNo);
}
if (string.IsNullOrEmpty(myUserAddressTable.StreetName))
{
myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@streetName", myUserAddressTable.StreetName);
}
if (myUserAddressTable.CityCode == null){
myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@cityCode", myUserAddressTable.CityCode);
}
if (myUserAddressTable.DistrictCode == null){
myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", myUserAddressTable.DistrictCode);
}
if (myUserAddressTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", myUserAddressTable.CountryCode);
}
if (string.IsNullOrEmpty(myUserAddressTable.PostalCode))
{
myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@postalCode", myUserAddressTable.PostalCode);
}
if (myUserAddressTable.AddressTypeCode == null){
myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@addressTypeCode", myUserAddressTable.AddressTypeCode);
}
if (myUserAddressTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myUserAddressTable.Status);
}
if (myUserAddressTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myUserAddressTable.CreatedDateTime);
}
if (myUserAddressTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myUserAddressTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a UserAddressTable from the database.
/// </summary>
/// <param name="userAddressKey">The UserAddressKey of the UserAddressTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int userAddressKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocUserAddressTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@userAddressKey", userAddressKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the UserAddressTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static UserAddressTable FillDataRecord(IDataRecord myDataRecord )
{
UserAddressTable myUserAddressTable = new UserAddressTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAddressKey")))
{
myUserAddressTable.UserAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAddressKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myUserAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
{
myUserAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LevelNo")))
{
myUserAddressTable.LevelNo = myDataRecord.GetString(myDataRecord.GetOrdinal("LevelNo"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
{
myUserAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
{
myUserAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
myUserAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
myUserAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
{
myUserAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
{
myUserAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myUserAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myUserAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myUserAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myUserAddressTable;
}
}

 } 
